from django.db import models


class Menu(models.Model):  # 菜单表
    title = models.CharField(verbose_name='一级菜单', max_length=32)
    icon = models.CharField(verbose_name='图标', max_length=32)

    def __str__(self):
        return self.title


class Permission(models.Model):  # 权限表
    title = models.CharField(verbose_name='权限', max_length=32)
    url = models.CharField(verbose_name='URL', max_length=128)
    name = models.CharField(verbose_name='url的别名', max_length=32, unique=True)
    menu = models.ForeignKey('Menu', blank=True, null=True, help_text='null表示不是菜单', on_delete=models.SET_NULL)
    p = models.ForeignKey('Permission', blank=True, null=True, related_name='pp', on_delete=models.SET_NULL)

    def __str__(self):
        return self.title


class Role(models.Model):  # 角色表
    title = models.CharField(verbose_name='角色', max_length=32)
    permissions = models.ManyToManyField(verbose_name='拥有的权限', to='Permission', blank=True)

    def __str__(self):
        return self.title


class UserInfo(models.Model):  # 用户表
    name = models.CharField(verbose_name='姓名', max_length=32)
    password = models.CharField(verbose_name='密码', max_length=64)
    email = models.CharField(verbose_name='邮箱', max_length=32)
    roles = models.ManyToManyField(verbose_name='拥有的角色', to='Role', blank=True)
    ###

    nickname = models.CharField(verbose_name='用户名', max_length=32)
    phone = models.CharField(verbose_name='手机号', max_length=32)

    gender_choices = (
        (1, '男'),
        (2, '女'),
    )
    gender = models.IntegerField(verbose_name='性别', choices=gender_choices, default=1)

    department = models.ForeignKey(verbose_name='部门', to="app_main.Department", blank=True, null=True,
                                   on_delete=models.SET_NULL)

    def __str__(self):
        return self.name
